﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Dare.Utilities
{
    public sealed class EnumAttributeWrapper<E, A>
        where E : struct
        where A : Attribute
    {
        private A attr;
        public A Attribute
        {
            get { return attr; }
        }

        private E value;
        public E Value
        {
            get { return this.value; }
        }

        public EnumAttributeWrapper(E val)
        {
            value = val;

            FieldInfo fieldinfo = val.GetType().GetField(val.ToString());
            object[] objs = fieldinfo.GetCustomAttributes(typeof(A), false);
            if (objs == null || objs.Length == 0)
            {
                attr = null;
            }
            else
            {
                attr = objs[0] as A;
            }
        }

        public override string ToString()
        {
            return attr == null ? String.Empty : attr.ToString();
        }

        /// <summary>
        /// 获得枚举所有值的属性包装类对象列表
        /// </summary>
        /// <typeparam name="E">枚举</typeparam>
        /// <typeparam name="A">枚举属性类</typeparam>
        /// <returns></returns>
        public static List<EnumAttributeWrapper<E, A>> GetEnumAttributeWrapperList()
        {
            E[] enums = (E[])Enum.GetValues(typeof(E));
            List<EnumAttributeWrapper<E, A>> list = new List<EnumAttributeWrapper<E, A>>(enums.Length);
            for (int i = 0; i < enums.Length; i++)
            {
                list.Add(new EnumAttributeWrapper<E, A>(enums[i]));
            }
            return list;
        }

        /// <summary>
        /// 获得所有枚举属性类对象列表
        /// </summary>
        /// <typeparam name="E">枚举</typeparam>
        /// <typeparam name="A">枚举属性类</typeparam>
        /// <returns></returns>
        public static List<A> GetEnumAttributeList()
        {
            E[] enums = (E[])Enum.GetValues(typeof(E));
            List<A> list = new List<A>(enums.Length);
            for (int i = 0; i < enums.Length; i++)
            {
                list.Add(new EnumAttributeWrapper<E, A>(enums[i]).Attribute);
            }
            return list;
        }

        public static E GetEnumItem(A arr)
        {
            E[] enums = (E[])Enum.GetValues(typeof(E));
            E result = default(E);
            for (int i = 0; i < enums.Length; i++)
            {
                if (arr.Equals(new EnumAttributeWrapper<E, A>(enums[i]).Attribute))
                {
                    result = new EnumAttributeWrapper<E, A>(enums[i]).Value;
                    break;
                }
            }
            return result;
        }

        public static E GetEnumItem(string arr)
        {
            E[] enums = (E[])Enum.GetValues(typeof(E));
            E result = default(E);
            for (int i = 0; i < enums.Length; i++)
            {
                if (new EnumAttributeWrapper<E, A>(enums[i]).Attribute.Equals(arr))
                {
                    result = new EnumAttributeWrapper<E, A>(enums[i]).Value;
                    break;
                }
            }
            return result;
        }

    }
}
